package com.scs.application.config;

import com.alibaba.fastjson.serializer.DoubleSerializer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.scs.application.core.utils.SpringUtils;
import com.scs.application.modules.sys.properties.ResProperties;
import com.scs.application.modules.thirdparty.properties.MscmProperties;
import com.scs.application.modules.fsd.thirdparty.properties.SPDProperties;
import org.apache.catalina.connector.Connector;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * @Description：公用bean配置
 */
@EnableAsync
@Configuration
@MapperScan(value = {"com.scs.application.modules.*.mapper","com.scs.application.thirdparty.*.*.mapper"})
@EnableConfigurationProperties({MscmProperties.class, ResProperties.class, SPDProperties.class})
public class BeanConfig {


    @Bean
    public SpringUtils springUtils() {
        return new SpringUtils();
    }

    @Bean
    public ForkJoinPool forkJoinPool() {
        return new ForkJoinPool();
    }


    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullNumberAsZero
//                SerializerFeature.WriteNullStringAsEmpty
        );

        // fastjson默认为科学计数法，现在使用保留小数后四位
        fastJsonConfig.getSerializeConfig().put(Double.class, new DoubleSerializer("#.####"));

        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        fastMediaTypes.add(MediaType.ALL);

        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        return fastJsonHttpMessageConverter;
    }

    /**
     * quartz 定制化配置
     *
     * @return
     */
    @Bean
    public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer() {
        return schedulerFactoryBean -> {
            // 设置不自动启动
            schedulerFactoryBean.setStartupDelay(10);
//            schedulerFactoryBean.setAutoStartup(true);
        };
    }

    @Bean
    public TomcatServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers((Connector connector) -> {
            connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}\\[\\]");
            connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}\\[\\]");
        });
        return factory;
    }


}
